package yamen.android.itscool;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Text;

import android.R.bool;
import android.R.integer;
import android.R.string;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.SystemClock;
import android.util.Log;
import android.widget.Toast;

public class ItsCoolNetstateReceiver extends BroadcastReceiver {

	String id;
	String passwd;
	SharedPreferences sp;
	private Context context;

	@Override
	public void onReceive(Context arg0, Intent arg1) {
		// TODO Auto-generated method stub
		
		context = arg0;
		sp = context.getSharedPreferences("ITS_COOL", context.MODE_PRIVATE);
		if (!sp.getBoolean("Action", false)) 
			return;
		
		
		ConnectivityManager cManager = (ConnectivityManager)arg0.getSystemService(Context.CONNECTIVITY_SERVICE);
		if(cManager == null)
			return;
		NetworkInfo ninfo = cManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if(ninfo == null)
			return;
		
		if(arg1.getAction().contains("CONNECT_ACTION"))
            sp.edit().putBoolean("Manual", true).commit();
		else
            sp.edit().putBoolean("Manual", false).commit();
				
		if(ninfo.isConnectedOrConnecting())
		{
			if(!ninfo.isConnected())
			{
                sp.edit().putBoolean("Showed", false).commit();
				return;
			}

            Intent intent = new Intent();
            intent.setClass(context, ItsCoolConnectService.class);
            context.startService(intent);
		}
		else 
            sp.edit().putBoolean("Showed", false).commit();
	}
}